home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / tde221.zip / PORT.C < prev    next >
C/C++ Source or Header  |  1993-04-01  |  9KB  |  266 lines

  1. /*
  2.  * Now that I own both MSC 7.0 and BC 3.1, lets rearrange stuff so both
  3.  * compilers can compile TDE.  Several implementation specific functions
  4.  * needed for both compilers were gathered into this file.
  5.  *
  6.  * Incidentally, there is difference between a NULL line pointer and
  7.  * a pointer to a line that contains no characters.  For example, calling
  8.  *
  9.  *       line = malloc( 0 );
  10.  *
  11.  *                   or, more precisely in TDE:
  12.  *
  13.  *       line = _fmalloc( 0 );
  14.  *       line = farmalloc( 0 );
  15.  *
  16.  * will return a valid pointer to an item of 0 length in some compilers
  17.  * and a NULL pointer in other compilers.  malloc( 0 ) will return a valid
  18.  * pointer to an object of zero length in MSC.  malloc( 0 ) will return a
  19.  * NULL pointer in BC.  The problem with returning a NULL pointer for
  20.  * malloc( 0 ) is that it's a little harder to tell if the heap is out of
  21.  * memory or if we have a valid NULL pointer.  On the other hand, the good
  22.  * part about returning a NULL pointer for malloc( 0 ) is that extra space
  23.  * is not wasted for an object of 0 length.  In TDE, we will test for 0
  24.  * before calling my_malloc( ) and set an ERROR code if out of memory.
  25.  *
  26.  * Although many PC C compilers have findfirst and findnext functions for
  27.  * finding files, let's write our own to keep a closer watch on
  28.  * critical errors.
  29.  *
  30.  *
  31.  * New editor name:  TDE, the Thomson-Davis Editor.
  32.  * Author:           Frank Davis
  33.  * Date:             June 5, 1991, version 1.0
  34.  * Date:             July 29, 1991, version 1.1
  35.  * Date:             October 5, 1991, version 1.2
  36.  * Date:             January 20, 1992, version 1.3
  37.  * Date:             February 17, 1992, version 1.4
  38.  * Date:             April 1, 1992, version 1.5
  39.  * Date:             June 5, 1992, version 2.0
  40.  * Date:             October 31, 1992, version 2.1
  41.  * Date:             April 1, 1993, version 2.2
  42.  *
  43.  * This code is released into the public domain, Frank Davis.
  44.  * You may use and distribute it freely.
  45.  */
  46.  
  47. #include "tdestr.h"
  48. #include "common.h"
  49. #include "tdefunc.h"
  50. #include "define.h"
  51.  
  52.  
  53. /*
  54.  * Name:    my_malloc
  55.  * Purpose: malloc from the far heap
  56.  * Date:    April 1, 1993
  57.  * Passed:  mem:  pointer to memory to free in far heap
  58.  *          rc:   pointer to return code
  59.  * Notes:   set the return code only if an ERROR occured with malloc.
  60.  *           returning a NULL pointer is not neccessarily an ERROR.
  61.  */
  62. void far * my_malloc( size_t size, int *rc )
  63. {
  64. void far *mem;
  65.  
  66.    assert( size < MAX_LINE_LENGTH );
  67.  
  68.    if (size == 0)
  69.  
  70.       /*
  71.        * if 0 bytes are requested, return NULL
  72.        */
  73.       mem = NULL;
  74.    else {
  75.  
  76. #if defined( __MSC__ )
  77.       mem = _fmalloc( size );
  78. #else
  79.       mem = farmalloc( size );
  80. #endif
  81.  
  82.       /*
  83.        * if malloc failed, return NULL and an ERROR.
  84.        */
  85.       if (mem == NULL)
  86.          *rc = ERROR;
  87.    }
  88.    return( mem );
  89. }
  90.  
  91.  
  92. /*
  93.  * Name:    my_free
  94.  * Purpose: free memory from the far heap
  95.  * Date:    April 1, 1993
  96.  * Passed:  mem:  pointer to memory to free in far heap
  97.  */
  98. void my_free( void far *mem )
  99. {
  100. #if defined( __MSC__ )
  101.    _ffree( mem );
  102. #else
  103.    farfree( mem );
  104. #endif
  105. }
  106.  
  107.  
  108. /*
  109.  * Name:    my_findfirst
  110.  * Purpose: find the first file matching a pattern using DOS interrupt
  111.  * Date:    January 6, 1992
  112.  * Passed:  dta:    disk transfer address
  113.  *          path:   path to search for files
  114.  *          f_attr: attributes of files to search for
  115.  * Notes:   return codes for my_findfirst:
  116.  *             0  no error
  117.  *             2  file is invalid or does not exist
  118.  *             3  path is invalid or does not exist
  119.  *            18  no matching directory entry was found
  120.  *            -1  check the critical error flag for critical errors
  121.  */
  122. int  my_findfirst( DTA far *dta, char far *path, int f_attr )
  123. {
  124. void far *old_dta;
  125. void far *new_dta;
  126. int  rc;
  127.  
  128.    new_dta = (void far *)dta;
  129.  
  130.    ASSEMBLE {
  131.  
  132. /*
  133. ; save the old dta
  134. */
  135.         mov     ah, 0x2f                /* DOS get dta */
  136.         int     0x21                    /* DOS interrupt */
  137.         mov     WORD PTR old_dta, bx    /* save OFFSET of old DTA */
  138.         mov     ax, es
  139.         mov     WORD PTR old_dta+2, ax  /* save SEGMENT of old DTA */
  140.  
  141. /*
  142. ; set the new dta
  143. */
  144.         push    ds                      /* save ds */
  145.         mov     dx, WORD PTR new_dta    /* get OFFSET of new dta */
  146.         mov     ax, WORD PTR new_dta+2  /* get SEGMENT of new dta */
  147.         mov     ds, ax                  /* put it in ds */
  148.         mov     ah, 0x1a                /* DOS set dta */
  149.         int     0x21                    /* DOS interrupt */
  150.         pop     ds                      /* get back ds */
  151.  
  152. /*
  153. ; find first matching file
  154. */
  155.         push    ds                      /* save ds */
  156.         mov     cx, WORD PTR f_attr     /* file attributes to search for */
  157.         mov     dx, WORD PTR path       /* get OFFSET of path */
  158.         mov     ax, WORD PTR path+2     /* get SEGMENT of path */
  159.         mov     ds, ax                  /* put it in ds */
  160.         mov     ah, 0x4e                /* DOS find first file */
  161.         int     0x21                    /* DOS interrupt */
  162.         pop     ds                      /* get back ds */
  163.  
  164. /*
  165. ; save the return code
  166. */
  167.         jc      an_error                /* carry is set if an error occured */
  168.         xor     ax, ax                  /* zero out ax, return OK if no error */
  169.    }
  170. an_error:
  171.  
  172.    ASSEMBLE {
  173.         mov     WORD PTR rc, ax         /* save the return code */
  174.  
  175. /*
  176. ; get back old dta
  177. */
  178.         push    ds                      /* save ds */
  179.         mov     dx, WORD PTR old_dta    /* get OFFSET of old dta */
  180.         mov     ax, WORD PTR old_dta+2  /* get SEGMENT of old dta */
  181.         mov     ds, ax                  /* put it in ds */
  182.         mov     ah, 0x1a                /* DOS set dta */
  183.         int     0x21                    /* DOS interrupt */
  184.         pop     ds                      /* get back ds */
  185.    }
  186.    if (ceh.flag == ERROR)
  187.       rc = ERROR;
  188.    return( rc );
  189. }
  190.  
  191.  
  192. /*
  193.  * Name:    my_findnext
  194.  * Purpose: find the next file matching a pattern using DOS interrupt
  195.  * Date:    January 6, 1992
  196.  * Passed:  dta:  disk transfer address
  197.  * Notes:   my_findfirst() MUST be called before calling this function.
  198.  *          return codes for my_findnext (see DOS tech ref manuals):
  199.  *             0  no error
  200.  *             2  path is invalid or does not exist
  201.  *            18  no matching directory entry was found
  202.  *            -1  check the critical error flag for critical errors
  203.  */
  204. int  my_findnext( DTA far *dta )
  205. {
  206. void far *old_dta;
  207. void far *new_dta;
  208. int  rc;
  209.  
  210.    new_dta = (void far *)dta;
  211.  
  212.    ASSEMBLE {
  213.  
  214. /*
  215. ; save the old dta
  216. */
  217.         mov     ah, 0x2f                /* DOS get dta */
  218.         int     0x21                    /* DOS interrupt */
  219.         mov     WORD PTR old_dta, bx    /* save OFFSET of old DTA */
  220.         mov     ax, es
  221.         mov     WORD PTR old_dta+2, ax  /* save SEGMENT of old DTA */
  222.  
  223. /*
  224. ; set the new dta
  225. */
  226.         push    ds                      /* save ds */
  227.         mov     dx, WORD PTR new_dta    /* get OFFSET of new dta */
  228.         mov     ax, WORD PTR new_dta+2  /* get SEGMENT of new dta */
  229.         mov     ds, ax                  /* put it in ds */
  230.         mov     ah, 0x1a                /* DOS set dta */
  231.         int     0x21                    /* DOS interrupt */
  232.         pop     ds                      /* get back ds */
  233.  
  234. /*
  235. ; find next matching file
  236. */
  237.         mov     ah, 0x4f                /* DOS find first file */
  238.         int     0x21                    /* DOS interrupt */
  239.  
  240. /*
  241. ; save the return code
  242. */
  243.         jc      an_error                /* carry is set if an error occured */
  244.         xor     ax, ax                  /* zero out ax, return OK if no error */
  245.    }
  246. an_error:
  247.  
  248.    ASSEMBLE {
  249.         mov     WORD PTR rc, ax         /* save the return code */
  250.  
  251. /*
  252. ; get back old dta
  253. */
  254.         push    ds                      /* save ds */
  255.         mov     dx, WORD PTR old_dta    /* get OFFSET of old dta */
  256.         mov     ax, WORD PTR old_dta+2  /* get SEGMENT of old dta */
  257.         mov     ds, ax                  /* put it in ds */
  258.         mov     ah, 0x1a                /* DOS set dta */
  259.         int     0x21                    /* DOS interrupt */
  260.         pop     ds                      /* get back ds */
  261.    }
  262.    if (ceh.flag == ERROR)
  263.       rc = ERROR;
  264.    return( rc );
  265. }
  266.